package com.hi.hiparty.ui.session;

import com.hi.hiparty.util.Constants;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class UserSessionManager {

	private static UserSession userSession = null;
	
	public static UserSession createNewSession(){
		userSession = new UserSession();
		return userSession;
	}

	public static UserSession getUserSession(Context context) {
		if (null == userSession) {
			SharedPreferences sp = context.getSharedPreferences(Constants.SP_KEY_USERINFO, Context.MODE_PRIVATE);
			String userid = sp.getString(Constants.SP_KEY_USERINFO_ID, null);
			if (null == userid) {
				return null;
			}
			createNewSession();
			userSession.setUserid(userid);
			userSession.setUsername(sp.getString(Constants.SP_KEY_USERINFO_NAME, null));
			userSession.setUserProfileImage(sp.getString(Constants.SP_KEY_USERINFO_PROFILEIMAGE, null));
			userSession.setLatlng(sp.getString(Constants.SP_KEY_USERINFO_LOCATION, Constants.DEFAULT_LOCATION));
			userSession.setUpdateTime(sp.getLong(Constants.SP_KEY_USERINFO_UPDATE_TIME, 0L));
		}
		return userSession;
	}

	//更新user avatar
	public static boolean updataUserAvatar(Context context,String avatar){
		if (null != getUserSession(context)) {
			userSession.setUserProfileImage(avatar);
			SharedPreferences sp = context.getSharedPreferences(Constants.SP_KEY_USERINFO, Context.MODE_PRIVATE);
			Editor ed = sp.edit();
			ed.putString(Constants.SP_KEY_USERINFO_PROFILEIMAGE, avatar);
			long time = System.currentTimeMillis();
			userSession.setUpdateTime(time);
			ed.putLong(Constants.SP_KEY_USERINFO_UPDATE_TIME, time);
			ed.commit();
		}
		return true;
	}
	
	public static boolean updateUserLocation(Context context, double latitude, double longtitude) {
		String loc = latitude + "," + longtitude;
		if (null != getUserSession(context)) {
			userSession.setLatlng(loc);
			SharedPreferences sp = context.getSharedPreferences(Constants.SP_KEY_USERINFO, Context.MODE_PRIVATE);
			Editor ed = sp.edit();
			ed.putString(Constants.SP_KEY_USERINFO_LOCATION, loc);
			long time = System.currentTimeMillis();
			userSession.setUpdateTime(time);
			ed.putLong(Constants.SP_KEY_USERINFO_UPDATE_TIME, time);
			ed.commit();
		}
		return false;
	}

	public static boolean saveUserSession(Context context) {
		if (null != getUserSession(context)) {
			SharedPreferences sp = context.getSharedPreferences(Constants.SP_KEY_USERINFO, Context.MODE_PRIVATE);
			Editor ed = sp.edit();
			if (null != userSession.getUserid()) {
				ed.putString(Constants.SP_KEY_USERINFO_ID, userSession.getUserid());
			}
			if (null != userSession.getUsername()) {
				ed.putString(Constants.SP_KEY_USERINFO_NAME, userSession.getUsername());
			}
			if (null != userSession.getUserProfileImage()) {
				ed.putString(Constants.SP_KEY_USERINFO_PROFILEIMAGE, userSession.getUserProfileImage());
			}
			if (null != userSession.getLatlng()) {
				ed.putString(Constants.SP_KEY_USERINFO_LOCATION, userSession.getLatlng());
			}
			long time = System.currentTimeMillis();
			userSession.setUpdateTime(time);
			ed.putLong(Constants.SP_KEY_USERINFO_UPDATE_TIME, time);
			ed.commit();
		}
		return false;
	}
}
